# coding=utf-8
# coding=utf-8
# coding=utf-8
# python编程作业：图书管理系统
import json
import os


# 定义一个book类

class Book:
    def __init__(self,id,name,price,summary):
        self.id = id
        self.name = name
        self.price = price
        self.summary = summary

    # 重写对象的显示格式方法
    def __str__(self):
        return f"SID: {self.id} --- Name: {self.name} --- Price: {self.price} --- Summary: {self.summary}"


class BookMannagement:

    # 实例化时构造一个变量
    def __init__(self):
        self.books = []


    # 菜单函数，输出打印信息，接收用户输入
    def menu(self):
        print("*****************************")
        print("*      图书管理系统           *")
        print("* 1. 添加新图书信息           *")
        print("* 2. 通过图书ID修改图书信息      *")
        print("* 3. 通过图书ID删除图书信息      *")
        print("* 4. 通过书名删除图书信息      *")
        print("* 5. 通过图书ID查询图书信息      *")
        print("* 6. 通过书名查询图书信息      *")
        print("* 7. 显示所有图书信息         *")
        print("* 8. 退出系统                *")
        print("*****************************")
        select_num = input("请输入你的选择：")
        return select_num


    # 书的ID
    def __getID(self):
        id = input("请输入图书ID：")
        return id


    # 书名
    def __getName(self):
        name = input("请输入图书名称：")
        return name


    # 价格
    def __getPrice(self):
        while True:
            price = input("请输入图书价格：")
            if price.isdigit():  # 判断输入的是否是纯数字
                return int(price)
            else:
                print("价格输入错误，请重新输入")


    # 图书简介
    def __getSummary(self):
        summary = input("请输入图书简介：")
        return summary


    # 添加图书
    def addBook(self, id, name, price, summary):
        for book in self.books:
            if book.id == id:
                print("图书的ID已存在，添加图书失败")
                return "图书添加失败"
        else:
            book = Book(id, name, price, summary)
            self.books.append(book)
            print("图书添加成功", book)
            return "图书添加成功"


    # 根据id修改图书
    def modifyBookByID(self,id,name,price,summary):
        for book in self.books:
            if book.id == id:
                book.name = name
                book.price = price
                book.summary = summary
                print("图书修改成功")
                return "图书修改成功"
        else:
            print(f"图书id{id}不存在,修改失败")
            return "图书修改失败"


    # 根据id删除图书
    def deleteBookByID(self, id):
        for book in self.books:
            if book.id == id:
                self.books.remove(book)
                print("图书删除成功")
                return "图书删除成功"
        else:
            print(f"图书id{id}不存在,删除失败")
            return "图书删除失败"


    # 根据书名删除图书
    def deleteBookByName(self,name):
        del_book = []
        for book in self.books:
            if book.name == name:
                del_book.append(book)
        if len(del_book) > 0:
            for book in del_book:
                self.books.remove(book)
                print(f"书名为{name}的图书删除成功")
            return "删除图书成功"
        else:
            print(f"书名为{name}的图书不存在，删除图书失败")
            return "删除图书失败"


    # 通过id查询图书
    def queryBookByID(self,id):
        for book in self.books:
            if book.id == id:
                print("根据id查询图书成功", book)
                return "根据id查询图书成功"
        else:
            print("根据id图书查询失败")
            return f"根据id查询图书失败,没有id为{id}的图书"


    # 根据书名查询图书
    def queryBookByName(self,name):
        sucess = False
        for book in self.books:
            if book.name == name:
                print("根据书名查询图书成功", book)
                sucess = True
        if sucess:
            return f"根据书名查询图书成功"
        else:
            print("根据书名查询图书失败")
            return f"根据书名查询图书失败,没有书名为{name}的图书"


    # 显示所有图书信息
    def showAllInfo(self):
        for book in self.books:
            print(book)
        return "图书显示成功"


    # 图书系统业务管理
    def bookManager(self):
        while True:
            select_num = self.menu()
            if len(select_num) == 1 and select_num in "12345678":
                if select_num == "1":
                    id = self.__getID()
                    name = self.__getName()
                    price = self.__getPrice()
                    summary = self.__getSummary()
                    self.addBook(id, name, price, summary)
                elif select_num == "2":
                    id = self.__getID()
                    name = self.__getName()
                    price = self.__getPrice()
                    summary = self.__getSummary()
                    self.modifyBookByID(id,name, price, summary)
                elif select_num == "3":
                    id = self.__getID()
                    self.deleteBookByID(id)
                elif select_num == "4":
                    name = self.__getName()
                    self.deleteBookByName(name)
                elif select_num == "5":
                    id = self.__getID()
                    self.queryBookByID(id)
                elif select_num == "6":
                    name = self.__getName()
                    self.queryBookByName(name)
                elif select_num == "7":
                    self.showAllInfo()
                elif select_num == "8":
                    break
            else:
                print("请输入正确的选择：")


if __name__ == '__main__':

    BookMannagement().bookManager()
